<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ApiUser extends BaseModel
{
    protected $connection = 'api';
    protected $userTable = 'user';
    protected $userBlackListTable = 'user_blacklist';
    protected $financeRechargeOrderTable = 'finance_recharge_order';

    //主播审核
    public function anchorVerify($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['user_id']) || empty($data['identity_status'])) {
                $msg = '参数错误';
                break;
            }
            if(!in_array($data['identity_status'], [3, 4])) {
                $msg = '参数错误';
                break;
            }
            $userInfo = DB::connection($this->connection)->table($this->userTable)->where('user_id', $data['user_id'])->first();
            $userInfo = objToArray($userInfo);
            if($userInfo['identity_status'] == $data['identity_status']) {
                $code = 20000;
                $msg = '成功';
                break;
            }
            if($userInfo['identity_status'] != 2) {
                $msg = '用户不是待审核';
                break;
            }

            $res = curl_request(getKkaudioApiUrl() . '/inner/user/anchorVerify', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //主播取消认证
    public function anchorOff($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['user_id'])) {
                $msg = '参数错误';
                break;
            }
            $data['identity_status'] = 4;
            $userInfo = DB::connection($this->connection)->table($this->userTable)->where('user_id', $data['user_id'])->first();
            $userInfo = objToArray($userInfo);
            if($userInfo['identity_status'] == $data['identity_status']) {
                $code = 20000;
                $msg = '成功';
                break;
            }

            $res = curl_request(getKkaudioApiUrl() . '/inner/user/anchorVerify', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //主播列表
    public function anchorList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $user_id = $request->input('user_id');
        $identity_status = $request->input('identity_status');

        $query = DB::connection($this->connection)->table($this->userTable);
        $query = $query->where('identity_role', 2);
        if(!empty($user_id)) {
            $query = $query->where('user_id', $user_id);
        }
        if(!empty($identity_status)) {
            $query = $query->where('identity_status', $identity_status);
        }
        $result = $query->orderBy('updated_at', 'desc')->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        $anchorList = [];
        foreach($result['data'] as $val) {
            $anchorList[] = [
                'user_id' => $val['user_id'],
                'uuid' => $val['uuid'],
                'mobile_phone' => $val['mobile_phone'],
                'nickname' => $val['nickname'],
                'gender' => $val['gender'],
                'birthday' => $val['birthday'],
                'active_certify' => $val['active_certify'],
                'voice' => build_image($val['voice']),
                'voice_time' => $val['voice_time'],
                'identity_role' => $val['identity_role'],
                'identity_status' => $val['identity_status'],
                'sign' => $val['sign'],
                'updated_at' => strtotime($val['updated_at']),
            ];
        }
        $result['data'] = $anchorList;

        return $result;
    }

    //主播取消认证
    public function genderReset($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['user_id'])) {
                $msg = '参数错误';
                break;
            }
            $res = curl_request(getKkaudioApiUrl() . '/inner/user/genderReset', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //用户验证码重置
    public function smsCodeReset($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['user_id'])) {
                $msg = '参数错误';
                break;
            }
            $res = curl_request(getKkaudioApiUrl() . '/inner/user/smsCodeReset', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //手机号验证码重置
    public function phonoCodeReset($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['phone'])) {
                $msg = '参数错误';
                break;
            }
            $res = curl_request(getKkaudioApiUrl() . '/inner/user/phonoCodeReset', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //解除系统用户黑名单
    public function blackListDelete($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['user_id'])) {
                $msg = '参数错误';
                break;
            }
            $data['userId'] = $data['user_id'];
            $res = curl_request(getKkaudioApiUrl() . '/inner/user/blackListDelete', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //新增系统用户黑名单
    public function blackListAdd($data){
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($data['user_id'])) {
                $msg = '参数错误';
                break;
            }
            $data['userId'] = $data['user_id'];
            $res = curl_request(getKkaudioApiUrl() . '/inner/user/blackListAdd', $data, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
            
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //系统用户黑名单列表
    public function blackList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $status = $request->input('status', 0);
        $userId = $request->input('user_id', 0);

        $query = DB::connection($this->connection)->table($this->userBlackListTable)->orderBy('id', 'desc');
        if($status == 1) {
            $query = $query->where('status', 1);
        }
        if($status == 2) {
            $query = $query->where('status', 0);
        }
        if(!empty($userId)) {
            $query = $query->where('user_id', $userId);
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {
            $userIds = array_column($result['data'], 'user_id');
            $userList = DB::connection($this->connection)->table($this->userTable)->whereIn('user_id', $userIds)->get();
            $userList = array_column(objToArray($userList), null, 'user_id');

            //用户充值金额
            $rechargeList = DB::connection($this->connection)->table($this->financeRechargeOrderTable)
                ->whereIn('user_id', $userIds)
                ->where('status', 1)
                ->selectRaw('sum(price) as price,user_id')
                ->groupBy('user_id')
                ->get();
            $rechargeList = array_column(objToArray($rechargeList), null, 'user_id');

            foreach($result['data'] as $key=>$val) {
                $val['uuid'] = empty($userList[$val['user_id']]['uuid']) ? '' : $userList[$val['user_id']]['uuid'];
                $val['nickname'] = empty($userList[$val['user_id']]['nickname']) ? '' : $userList[$val['user_id']]['nickname'];
                $val['rechargePrice'] = empty($rechargeList[$val['user_id']]['price']) ? 0 : $rechargeList[$val['user_id']]['price'];

                $result['data'][$key] = $val;
            }
        }

        return $result;
    }

    //设置用户头像
    public function setHeadImg($params)
    {
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($params['user_id'])) {
                $msg = '参数错误';
                break;
            }
            $params['head_img'] = empty($params['head_img']) ? '' : parse_url(trim($params['head_img']), PHP_URL_PATH);
            if(empty($params['head_img'])) {
                $msg = '参数错误';
                break;
            }
            $res = DB::connection($this->connection)->table($this->userTable)->where('user_id', $params['user_id'])->select('portrait')->first();
            if(empty($res)) {
                $msg = '参数错误';
                break;
            }
            $portrait = json_decode($res->portrait, true);
            $portrait = empty($portrait) ? [] : $portrait;
            array_unshift($portrait, $params['head_img']);
            $images = [];
            foreach($portrait as $val) {
                $img = getUrlPath($val);
                if(!empty($img)) {
                    $images[] = build_image($img);
                }
            }

            $postData['user_id'] = $params['user_id'];
            $postData['images'] = implode(',', $images);
            $res = curl_request(getKkaudioApiUrl().'/inner/user/edit_user', $postData, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }

    //设置用户照片墙
    public function setUserPortrait($params)
    {
        $code = 50000;
        $msg = '失败';
        do {
            if(empty($params['user_id'])) {
                $msg = '参数错误';
                break;
            }
            if(empty($params['portraits'])) {
                $msg = '参数错误';
                break;
            }
            $portraits = explode(',', $params['portraits']);
            $newPortraits = [];
            foreach($portraits as $val) {
                $img = getUrlPath($val);
                if(!empty($img)) {
                    $newPortraits[] = build_image($img);
                }
            }

            $postData['user_id'] = $params['user_id'];
            $postData['images'] = implode(',', $newPortraits);
            $res = curl_request(getKkaudioApiUrl().'/inner/user/edit_user', $postData, 'json', 'POST');
            if(isset($res['code']) && $res['code']==0) {
                $code = 20000;
                $msg = '成功';
                break;
            }else{
                $msg = empty($res['msg']) ? '失败' : $res['msg'];
                break;
            }
        } while (false);

        return ['code'=>$code, 'msg'=>$msg];
    }



}
